Shader "Custom/BRDF1" {
	Properties {
		//_MainTex ("Base (RGB)", 2D) = "white" {}
		_Color("Color",color) = (1,1,1,1)
		_Ks("BRDF ks",float) = 0.5
		_Shininess("BRDF _Shininess",float) = 0.5
	}
	SubShader {
		Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"}
		LOD 200
		Pass{
			CGPROGRAM
			#include "UnityCG.cginc"
			#pragma vertex vert
			#pragma fragment frag
			
			float4 _LightColor0;
			float4 _Color;
			float _Ks;
			float _Shininess;
			
			struct v2f{
				float4 pos:POSITION;
				float3 worldPos:TEXCOORD0;
				float3 normal:TEXCOORD1;
			};
			
			v2f vert(appdata_base i){
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
				o.worldPos = mul(_Object2World,i.vertex).xyz;
				o.normal = normalize(mul(float4(i.normal,0),_World2Object).xyz);
				
				return o;
			}
			
			float4 frag(v2f i):COLOR{
				float4 color = float4(0);
				
				float3 N = normalize(i.normal);
				float3 L = normalize(_WorldSpaceLightPos0.xyz); //unity direction light.
				float3 V = normalize(_WorldSpaceCameraPos.xyz - i.worldPos);
				float3 T = normalize(cross(N,V));
				
				//diffuse
				float diff = max(0,dot(N,L));
				color = _LightColor0 * _Color * diff;
				
				//brdf
				bool back = ((dot(V,N) > 0 && (dot(L,N))));
				if(back){
					float a = dot(L,T);
					float b = dot(V,T);
					float c = sqrt(1-pow(a,2)) * sqrt(1-pow(b,2)) - a * b;
					float brdf = _Ks * pow(c,_Shininess);
					
					color += _Color * brdf;
				}
				
				return color;
			}
			
			ENDCG
		}
	} 
	// FallBack "Diffuse"
}
